var addhtml = ''
addhtml += '<div class="form-div"><form id="fomr" class="layui-form">';
addhtml += '<div class="layui-form-item"><label class="layui-form-label">管理员名称</label><div class="layui-input-block"><input type="text" name="username" autocomplete="off" class="layui-input"></div></div>';
addhtml += '<div class="layui-form-item"><label class="layui-form-label">手机号码</label><div class="layui-input-block"><input type="tel" name="phone" autocomplete="off" class="layui-input"></div></div>';
addhtml += '<div class="layui-form-item"><label class="layui-form-label">密码</label><div class="layui-input-block"><input type="password" name="password" autocomplete="off" class="layui-input"></div></div>';
addhtml += '<div class="layui-form-item"><label class="layui-form-label">重复密码</label><div class="layui-input-block"><input type="password" name="repassword" autocomplete="off" class="layui-input"></div></div>';
addhtml += '<div class="layui-form-item"><label class="layui-form-label">状态</label><div class="layui-input-block layui-form-select-icon iconfont"><select name="disable" lay-ignore class="layui-input disable-input"></select></div></div>';
addhtml += '<div class="layui-form-item"><label class="layui-form-label">用户组</label><div class="layui-input-block layui-form-select-icon iconfont"><select name="group_id" lay-ignore class="layui-input group-input"></select></div></div>';
addhtml += '<input type="hidden" name="token" value="' + token + '">';
addhtml += '</form></div>';

var eidthtml = ''
eidthtml += '<div class="form-div"><form id="fomr" class="layui-form">';
eidthtml += '<div class="layui-form-item"><label class="layui-form-label">管理员名称</label><div class="layui-input-block"><input type="text" name="username" autocomplete="off" class="layui-input"></div></div>';
eidthtml += '<div class="layui-form-item"><label class="layui-form-label">手机号码</label><div class="layui-input-block"><input type="tel" name="phone" autocomplete="off" class="layui-input"></div></div>';
eidthtml += '<div class="layui-form-item"><label class="layui-form-label">用户组</label><div class="layui-input-block layui-form-select-icon iconfont"><select name="group_id" lay-ignore class="layui-input group-input"></select></div></div>';
eidthtml += '<input type="hidden" name="token" value="' + token + '">';
eidthtml += '<input type="hidden" name="id">';
eidthtml += '</form></div>';

layui.use('table', function() {
	var table = layui.table,form = layui.form;
    
    
    //状态操作
	form.on('switch(disDemo)', function(obj) {
		var id = this.value;
		if(obj.elem.checked == true) {
			var disable = "0";
			var tel='您确定开启管理员';
		} else {
			var disable = "1";
			var tel='您确定禁用管理员';
		}
		layer.alert(tel,{
			icon: 2,
			btn: ['确定', '关闭'],
			cancel: function(){
				location.reload();
			},
			btn1: function() {
				$.ajax({
					type: 'post',
					dataType: 'json',
					url: editSrc,
					data: {
						id: id,
						disable: disable,
						token:token
					},
					success: function(data) {
						var code = data.code;
						var msg = data.msg;
						if(code == 1) {
							layer.close(layer.index)
							layer.alert(msg, {
								icon: 1
							})
						} else {
							layer.alert(msg, {
								icon: 5
							}, function() {
								location.reload();
							})
						}
					}
				})
			},
			btn2:function(){
				location.reload();
			}
		})
	});
    
    
	layer.msg('数据加载中...', {
		icon: 16,
		shade: 0.3,
		success: function() {
			table.render({
				elem: '#centet-cen',
				url: listSrc,
				height: $(".center-tab").height(),
				id: 'idTest',
				response: {
		  			statusCode: 1, //成功的状态码，默认：0
				},     
				page:true,
				cols: [
					[ //标题栏
						{
							field: 'id',
							title: 'ID',
							width:80,
							sort: true
						}, {
							field: 'username',
							title: '管理员名称'
						}, {
							field: 'disable',
							title: '状态',
							templet: '#switchTpl', 
							unresize: true,
							width: 150
						}, {
							field: 'phone',
							title: '电话号码'
						}, {
							field: 'group_name',
							title: '用户组'
						}, {
							field: 'create_date',
							title: '创建时间'
						}, {
							field: 'oper',
							title: '操作',
							width: 300,
							minWidth: 160,
							toolbar: '#barDemo'
						}

					]
				],
				limit: 10
			});
			layer.close(layer.index);
		}

	})
	table.on('tool(table)', function(obj) {
		var data = obj.data;
		if(obj.event === 'edit') {
			var id = data.id;
			$.ajax({
				type: 'post',
				dataType: 'json',
				url: seeSrc,
				data: {
					id: id
				},
				success: function(data) {
					layer.open({
						title: "编辑管理员",
						type: 1,
						skin: 'layui-layer-rim',
						area: ['550px', '300px'],
						content: eidthtml,
						btn: ['提交', '关闭'],
						btn1: function() {
							var Adata = $("#fomr").serialize();
							$.ajax({
								type: 'post',
								dataType: 'json',
								url: editSrc,
								data: Adata,
								success: function(data) {
									var code = data.code;
									var msg = data.msg;
									if(code == 1) {
										layer.close(layer.index)
										layer.alert(msg, {
											icon: 1
										}, function() {
											location.reload();
										})
									} else {
										layer.alert(msg, {
											icon: 5
										})
									}
								}
							})
						}
					});
					
					$("#fomr select.disable-input").html($("#disableHtml").html());
					$("#fomr select.group-input").html($("#groupHtml").html());
					$("#fomr").formEdit(data.data);
					form.render();
				}
			})
			
		}
	})
})


layui.use('layer', function() {
	var $ = layui.jquery,
		layer = layui.layer;
	var active = {
		add: function() {
			layer.open({
				type: 1,
				title: "新增会员",
				area: ['550px', '450px'], //宽高
				maxmin: true,
				resize: true,
				content: addhtml,
				btn: ['提交', '关闭'],
				btn1: function() {
					var Adata = $("#fomr").serialize();
					$.ajax({
						type: 'post',
						dataType: 'json',
						url: addSrc,
						data: Adata,
						success: function(data) {
							var code = data.code;
							var msg = data.msg;
							if(code == 1) {
								layer.close(layer.index)
								layer.alert(msg, {
									icon: 1
								}, function() {
									location.reload();
								})
							} else {
								layer.alert(msg, {
									icon: 5
								})
							}
						}
					})
				}
			});
			$("#fomr select.disable-input").html($("#disableHtml").html());
			$("#fomr select.group-input").html($("#groupHtml").html());
			form.render();

		}
	}
	$('.right-center').on('click', '.right-btn', function() {
		var othis = $(this),method = othis.data('method');
		active[method] ? active[method].call(this, othis) : '';
	});
})

$(function() {
	$.ajax({
		type: 'post',
		dataType: 'json',
		url: groupListSrc,
		success: function(data) {
			var groupHtml = '<option value="">请选择用户组</option>'
			$.each(data.data, function(index, value) {
				groupHtml += '<option value="' + index + '">' + value.group_name + '</option>';
			});
			$(".group-input").html(groupHtml);
			$("body").append('<select id="groupHtml">' + groupHtml + '</select>');
		}
	})
	
	$.ajax({
		type: 'post',
		dataType: 'json',
		url: disableSrc,
		success: function(data) {
			var disableHtml = '<option value="">请选择管理员状态</option>'
			$.each(data.data, function(index, value) {
				disableHtml += '<option value="' + index + '">' + value + '</option>';
			});
			$(".disable-input").html(disableHtml);
			$("body").append('<select id="disableHtml">' + disableHtml + '</select>');
		}

	})

})